<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80%" destroy-on-close @closed="$emit('closed')" :align-center="true" :close-on-click-modal="false">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="110px" label-position="right" style="height:350px;overflow:auto">
            <el-col :span="8">
                <el-form-item label="备注" prop="reason">
                    <el-input v-model="form.reason" placeholder="请输入" clearable autocomplete="off"></el-input>
                </el-form-item>
            </el-col>
            <el-form-item label="物料信息" prop="detail">
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                    <el-table-column prop="material_name" label="物料名称">
                        <template #default="scope">
                            <div>{{scope.row.material_name}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="material_spec_code" label="物料色号" align="center">
                        <template #default="scope">
                            <div>{{scope.row.material_spec_code}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="number" label="采购数量" align="center">
                        <template #default="scope">
                            <div>{{scope.row.number}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="residue" label="剩余入库数量" align="center">
                        <template #default="scope">
                            <div>{{scope.row.residue}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="warehouse_id" label="入库仓库" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.warehouse_id"  filterable clearable>
                                <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id"/>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="num" label="实际入库数量" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.num" placeholder="请输入入库数量"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="income_num" label="已入库数量" align="center">
                        <template #default="scope">
                            <div>{{scope.row.income_num}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注" align="center">
                        <template #default="scope">
                            <div>{{scope.row.remark}}</div>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">完 成</el-button>
		</template>
	</el-dialog>
</template>
<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加客户',
					edit: '入库',
					over: '完成打板'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
                    reason:"",
                    detail:[],
				},
				//验证规则
				rules: {
                    audit_status: [
						{required: true, message: '请选择'}
					],
				},
                show:false
			}
		},
		mounted() {
            this.getWare()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
                console.log('this.form :>> ', this.form);
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.material.getMaterialPurchaseOrder.editQuantity.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
                this.form.id = data.id
                var res = await this.$API.material.getMaterialPurchaseOrder.detail.get({id:data.id});
                // this.form.detail = res.data.detail
                let array = []
                res.data.detail.map(item=>{
                    array.push({
                        id:item.id,
                        num:"",
                        material_id:item.material_id,
                        order_sn:item.order_sn,
                        material_spec_code:item.material_spec_code,
                        material_name:item.material_name,
                        number:item.num,
                        remark:item.remark,
                        income_num:item.income_num,
                        residue:(item.num - item.income_num).toFixed(2)
                    })
                })
                this.form.detail = array
			},
            change(e){
                if(e==2){
                    this.show = true
                }else{
                    this.show = false
                }
            },
            // 获取仓库
            async getWare(){
                var res = await this.$API.all.ware.get();
                this.wareList = res.data;
            },
            
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
